package com.base.kit;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileManager {

    public static boolean mkDirs(String dirPath) {
        return mkDirs(new File(dirPath));
    }

    public static boolean mkDirs(File file) {
        try {
            return !file.exists() && file.mkdirs();
        } catch (Exception e) {
            return false;
        }
    }

    public static void write(String dirPath, String fileName, String content) {
        write(new File(dirPath, fileName), content);
    }

    public static void write(File file, String content) {
        write(file, content, false);
    }

    public static void write(File file, String content, boolean append) {
        try (FileOutputStream fos = new FileOutputStream(file, append);
             BufferedOutputStream outputStream = new BufferedOutputStream(fos)) {
            outputStream.write(content.getBytes());
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void write(InputStream in, File file) {
        try (FileOutputStream fos = new FileOutputStream(file)) {
            byte[] buffer = new byte[1024 * 128];
            int len;
            while ((len = in.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            StreamUtil.closeStream(in);
        }
    }

    public static String read(String dirPath, String fileName) {
        return read(new File(dirPath, fileName));
    }

    public static String read(File file) {
        try (FileInputStream fis = new FileInputStream(file)) {
            return StreamUtil.stream2String(fis);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static boolean delete(String filePath) {
        return delete(new File(filePath));
    }

    public static boolean delete(String dirPath, String fileName) {
        return delete(new File(dirPath, fileName));
    }

    /**
     * @param file Eg:"F:\ss\新建文本文档.txt"
     * @return 删除文件是否成功
     */
    public static boolean delete(File file) {
        try {
            return file.exists() && file.delete();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除掉目录下的空文件夹
     *
     * @param dirPath 文件夹路径
     */
    public static void delEmptyDirs(String dirPath) {
        delEmptyDirs(new File(dirPath));
    }

    public static void delEmptyDirs(File file) {
        if (!file.exists()) {
            return;
        }
        if (file.isDirectory()) {
            File[] childFiles = file.listFiles();
            if (childFiles == null || childFiles.length == 0) {
                delete(file);
            } else {
                for (File childFile : childFiles) {
                    delEmptyDirs(childFile);
                }
            }
        }
    }

    /**
     * 删除文件及所在文件夹
     */
    public static void deleteChildAndParent(String dirPath) {
        deleteChildAndParent(new File(dirPath));
    }

    /**
     * 文件和文件夹一起删除
     */
    public static void deleteChildAndParent(File file) {
        if (null == file)
            return;
        if (file.exists()) {
            if (file.isFile()) {
                delete(file);
                return;
            }
            if (file.isDirectory()) {
                File[] childFiles = file.listFiles();
                if (null != childFiles && childFiles.length != 0) {
                    for (File f : childFiles) {
                        deleteChildAndParent(f); //递归删除
                    }
                }
            }
            delete(file);
        }
    }

    /**
     * 删除文件夹下所有文件(不删除当前文件夹)
     */
    public static void deleteDirChild(String dirPath) {
        try {
            File dirFile = new File(dirPath);
            if (!dirFile.exists()) {
                mkDirs(dirFile);
                return;
            }
            File[] childFile = dirFile.listFiles();
            if (null != childFile && childFile.length > 0) {
                for (File child : childFile) {
                    delete(child);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
